//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ai\fn_artillery.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

_this spawn {
	TRACE_1("Spawn Start",_this);
	private ["_height", "_ari_type", "_radius", "_series", "_x1", "_y1", "_angle", "_i", "_j"
			,"_nos", "_shell", "_xo", "_pos", "_pod", "_obj", "_salvos","_shape"
			,"_rounds","_interval"];
	DEFAULT_PARAM(_target,0,objNull);
	DEFAULT_PARAM(_type,1,"");
	_radius = 30;
	_salvos = 2;
	_shape = [];
	if (IS_MARKER(_target) && {(count _this <= 4)}) then {
		_shape = [_target] call FFUNC(toShape);
		TRACE_1("Shape",_shape);
		_radius = (_shape select 1) max (_shape select 2);
		DEFAULT_PARAM(_salvos,2,2);
		DEFAULT_PARAM(_interval,3,30);
	} else {
		_shape = [_target] call FFUNC(toShape);
		TRACE_1("Shape",_shape);
		DEFAULT_PARAM(_radius,2,30);
		DEFAULT_PARAM(_salvos,3,2);
		DEFAULT_PARAM(_interval,4,30);
	};
	
	if (!IS_SCALAR(_radius) || {_radius < 30}) then { _radius = 30; };
	if (!IS_SCALAR(_salvos) || {_salvos < 1}) then { _salvos = 1; };
	if (!IS_SCALAR(_salvos) || {_salvos > 10}) then { _salvos = 10; };
	if (!IS_SCALAR(_interval) || {_interval < 10}) then { _interval = 10; };

	_magammoconf = _type;
	if !(isClass(configFile/"CfgAmmo"/_magammoconf)) then {
		_magammoconf = switch(toLower(_type)) do {
			case "cluster": {"Cluster_155mm_AMOS"};
			case "smoke": {"Smoke_120mm_AMOS_White"};
			case "fumi": {"Smoke_120mm_AMOS_White"};
			case "flare": {"Flare_82mm_AMOS_White"};
			default {"Sh_155mm_AMOS"};
		};
	};
	_inrange = true;

	_ammoconf = configFile/"CfgAmmo"/_magammoconf;
	_is_flare = getText(_ammoconf/"effectFlare") == "CounterMeasureFlare";
	_is_smoke = getText(_ammoconf/"submunitionAmmo") == "SmokeShellArty";

	_series = 0;
	_rounds = 1;
	
	_shape = [_target] call FFUNC(toShape);
	_at_pos = _shape select 0;
	_at_pos set [2,0];
	TRACE_1("Fire",ARR_5(_at_pos,_magammoconf,_is_flare,_is_smoke,_salvos));
	for "_series" from 1 to _salvos do {
		sleep ((_interval * 0.7) + random (_interval / 3));
		
		_height = 150;
		_shell = objNull;
		_rounds = 3;
		if (!_is_flare && {!_is_smoke}) then {
		
		} else {
			if (_is_flare) then {
				_height = 200;
			} else {
				_rounds = 6;
				_height = 1;		
			};
		};
		for "_i" from 0 to _rounds do {
			_radius = 30 + random 20;
			_angle = floor random 360;
			_x1 = (_at_pos select 0) - ((random _radius) * sin _angle);
			_y1 = (_at_pos select 1) - ((random _radius) * cos _angle);
			sleep 0.2;
			_at_pos set [2,_height];
			_shell = createVehicle [_magammoconf, [_x1, _y1, _height], [], 0, "NONE"];
			if (_is_flare) then {
				_shell setVelocity [0,0,-20];
			} else {
				_shell setVelocity [0,0,-150];
			};
			sleep 0.5;
		};		
		
		if (_series < _salvos) then {
			_shape = [_target] call FFUNC(toShape);
			_at_pos = _shape select 0;
			_at_pos set [2,0];
		};
	};
	TRACE("Spawn End");
};
TRACE("End");
TMFPROFILERSTOP;